package com.support.android.myapp3.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.support.android.myapp3.R;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PriceCalcFragment extends Fragment {
    private View view;
    private EditText priceEdit;
    private ListView listView;

    private static PriceCalcFragment fa;
    public static PriceCalcFragment getInstance() {
        if (fa == null) {
            fa = new PriceCalcFragment();
        }
        return fa;
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view= inflater.inflate(R.layout.fragment_price, container, false);

        Button calcBtn = (Button) view.findViewById(R.id.btn_click_2);
        listView = (ListView) view.findViewById(R.id.listView);
        priceEdit = (EditText) view.findViewById(R.id.edit_price);

        calcBtn.setOnClickListener(new MyonclickListener());

        return   view;
    }

    private class MyonclickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            try {
                String priceStr = priceEdit.getText().toString();
                //判断有无输入
                if (TextUtils.isEmpty(priceStr)) {
                    Toast.makeText(getActivity(), "没有数据输入", Toast.LENGTH_LONG).show();
                    return;
                }

                String price = priceEdit.getText().toString();

                try {
                    BigDecimal p = new BigDecimal(price);
                }catch (Exception e){
                    Toast.makeText(getActivity(), String.format("请输入合法的价格(%s)",price), Toast.LENGTH_SHORT).show();
                }

                Toast.makeText(getActivity(), priceStr, Toast.LENGTH_SHORT).show();
                List<Map<String, String>> data = calcListData(price);
////
                SimpleAdapter adapter = new SimpleAdapter(getActivity(), data, R.layout.item_price,
                        new String[]{"p_down", "p_up"}, new int[]{R.id.p_down, R.id.p_up});
//
                listView.setAdapter(adapter);

            } catch (Exception ex) {
                ex.printStackTrace();
                Toast.makeText(getActivity(), "系统处理异常", Toast.LENGTH_LONG).show();
            }

        }
    }

    public List<Map<String, String>> calcListData(String price) {
        List<Map<String, String>> data = new ArrayList<>();
        float step = 0.5f;
        float init = 0;

        while (init < 10) {
            init += 0.5;
            HashMap<String, String> item = new HashMap<String, String>();
            item.put("p_down", calcPriceDown(price,init));
            item.put("p_up", calcPriceUp(price,init));
            data.add(item);

        }

        return data;
    }

    public String calcPriceUp(String price, float step) {
        BigDecimal p = new BigDecimal(price);
        BigDecimal s = new BigDecimal(step);

        return p.multiply(s.divide(new BigDecimal("100"))).add(p).
                setScale(3, BigDecimal.ROUND_HALF_DOWN).toString() + String.format("(+%s%%)", step);
    }

    public String calcPriceDown(String price, float step) {
        BigDecimal p = new BigDecimal(price);
        BigDecimal s = new BigDecimal(step);

        return p.subtract(p.multiply(s.divide(new BigDecimal("100")))).
                setScale(3, BigDecimal.ROUND_HALF_DOWN).toString() + String.format("(-%s%%)", step);
    }


}
